#include "user_serial.h"

int fputc(int ch, FILE *f)
{
	HAL_UART_Transmit(&Debug_SERIAL, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
	return ch;
}

void serial_init(void)
{
	serial_initDebug();
	serial_initRobotArm();
	serial_initHIMI();
}


// HIMI Start========================
#define HIMI_BUFFER_SIZE 64
static uint8_t HIMIReceiveBuffer[HIMI_BUFFER_SIZE] = {0};
static uint16_t HIMIReceiveSize = 0;

extern DMA_HandleTypeDef HIMI_SERIAL_DMA;

void serial_initHIMI(void)
{
	
	HAL_UARTEx_ReceiveToIdle_DMA(&HIMI_SERIAL, (uint8_t *)HIMIReceiveBuffer, HIMI_BUFFER_SIZE);
	__HAL_DMA_DISABLE_IT(&HIMI_SERIAL_DMA, DMA_IT_HT);
	
}

void serial_receiveHIMI(UART_HandleTypeDef *huart, uint16_t Size)
{
	if (huart == &HIMI_SERIAL)
	{
		HIMIReceiveSize = Size;
		HIMIReceiveBuffer[Size] = '\0';
		serial_initHIMI();
	}
}

uint8_t * serial_bufferHIMI(void){
	
	return HIMIReceiveBuffer;
	
}

uint16_t serial_sizeHIMI(void){
	
	return HIMIReceiveSize;
	
}

// HIMI END========================

// RobotArm Start========================
#define RobotArm_BUFFER_SIZE 64
static uint8_t RobotArmReceiveBuffer[RobotArm_BUFFER_SIZE] = {0};
static uint16_t RobotArmReceiveSize = 0;

extern DMA_HandleTypeDef RobotArm_SERIAL_DMA;

void serial_initRobotArm(void)
{
	
	HAL_UARTEx_ReceiveToIdle_DMA(&RobotArm_SERIAL, (uint8_t *)RobotArmReceiveBuffer, RobotArm_BUFFER_SIZE);
	__HAL_DMA_DISABLE_IT(&RobotArm_SERIAL_DMA, DMA_IT_HT);
	
}

void serial_receiveRobotArm(UART_HandleTypeDef *huart, uint16_t Size)
{
	if (huart == &RobotArm_SERIAL)
	{
		RobotArmReceiveSize = Size;
		RobotArmReceiveBuffer[Size] = '\0';
		serial_initRobotArm();
	}
}

uint8_t * serial_bufferRobotArm(void){
	
	return RobotArmReceiveBuffer;
	
}

uint16_t serial_sizeRobotArm(void){
	
	return RobotArmReceiveSize;
	
}

// RobotArm END========================

// Debug Start========================
#define Debug_BUFFER_SIZE 64
static uint8_t DebugReceiveBuffer[Debug_BUFFER_SIZE] = {0};
static uint16_t DebugReceiveSize = 0;

extern DMA_HandleTypeDef Debug_SERIAL_DMA;

void serial_initDebug(void)
{
	
	HAL_UARTEx_ReceiveToIdle_DMA(&Debug_SERIAL, (uint8_t *)DebugReceiveBuffer, Debug_BUFFER_SIZE);
	__HAL_DMA_DISABLE_IT(&Debug_SERIAL_DMA, DMA_IT_HT);
	
}

void serial_receiveDebug(UART_HandleTypeDef *huart, uint16_t Size)
{
	if (huart == &Debug_SERIAL)
	{
		DebugReceiveSize = Size;
		DebugReceiveBuffer[Size] = '\0';
		serial_initDebug();
	}
}

uint8_t * serial_bufferDebug(void){
	
	return DebugReceiveBuffer;
	
}

uint16_t serial_sizeDebug(void){
	
	return DebugReceiveSize;
	
}

// Debug END========================


